package com.tyc.mb.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.IOException;

/**
 * 多数据源配置
 *
 * @author tyc
 * @version 1.0
 * @date 2021-10-25 18:00:10
 */
@Configuration
public class DataSourceConfig {
    @Autowired
    private DynamicDataSource dynamicDataSource;

    @Bean
    public SqlSessionFactoryBean userSqlSessionFactory() throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dynamicDataSource);
        // 配置mapper xml文件路径 通过application.yml配置不起作用
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*.xml"));
        return sqlSessionFactoryBean;
    }
}
